package ij.hkust.gui.laserCtr.DAOSTORM;

import ij.IJ;
import ij.hkust.Constants;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * the DAOSTORM laser we inherit from JPanel
 * @author liuquan
 *
 */
public class DSTLaser extends JPanel{
	
	private static final long serialVersionUID = 1L;
	
	//the data we need
	private String laser = "";//the DAOSTORM laser
	private String intensity = "0";//the laser'intensity
	private int index;//the index in parent
	private boolean isSelected = false;
	
	private int statu = 0;
	//the component in the Panel
	private JRadioButton selectFlag= new JRadioButton("");
	private JSlider intensitySlider = new JSlider();
	private JLabel intensityLabel = new JLabel("0%");
	private JComboBox lasersCb = new JComboBox();
	private JButton delB = new JButton("delete");
	
	public DSTLaser(String la,String inten) {
		
//		this.setBounds(0, 0, 500, 50);
		this.setBackground(Color.LIGHT_GRAY);
		lasersCb.setModel(new DefaultComboBoxModel(new String[] {"","405", "459", "488", "532", "561", "656"}));
		lasersCb.setSelectedItem(la);
		laser = la;
		intensity = inten;
		intensitySlider.setValue(Integer.parseInt(inten)*100/Constants.LASER_MAX_POWER);
		intensityLabel.setText(Integer.parseInt(inten)*100/Constants.LASER_MAX_POWER + "%");
		
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				setSelected(true);
				DSTEditor.instance.changeSelect(index);
			}
		});
		setLayout(null);
		selectFlag.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				setSelected(true);
			}
		});
		selectFlag.setBackground(Color.LIGHT_GRAY);
		
		selectFlag.setBounds(6, 6, 30, 23);
		add(selectFlag);
		intensitySlider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				JSlider tmp = (JSlider)arg0.getSource();
				intensityLabel.setText(tmp.getValue() + "%");
				intensity = tmp.getValue() * Constants.LASER_MAX_POWER / 100 + "";
				setSelected(true);
			}
		});
		intensitySlider.setBackground(Color.LIGHT_GRAY);
		
		intensitySlider.setBounds(42, 6, 200, 23);
		add(intensitySlider);
		intensityLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				setSelected(true);
			}
		});
		
		intensityLabel.setBounds(252, 10, 39, 15);
		add(intensityLabel);
		lasersCb.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				JComboBox tmp = (JComboBox) arg0.getSource();
				
				//before we change the laser, we should check if there already have the same laser
				if(DSTEditor.instance.isCanChange(index, tmp.getSelectedItem().toString())){
					laser = tmp.getSelectedItem().toString();
				}else{
					if(statu == 0){
						IJ.error("Wrong Laser", "There already have laser : " + tmp.getSelectedItem().toString());
						statu = 1;
					}else{
						statu = 0;
					}
					tmp.setSelectedItem(laser);
				}
				setSelected(true);
			}
		});
		lasersCb.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				setSelected(true);
			}
		});
		lasersCb.setBackground(Color.LIGHT_GRAY);
		lasersCb.setBounds(301, 7, 54, 21);
		add(lasersCb);
		
		delB.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//here we delete this laser from the window
				DSTEditor.instance.delLaser(index);
			}
		});
		delB.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				setSelected(true);
			}
		});
		delB.setBackground(Color.LIGHT_GRAY);
		
		delB.setBounds(365, 6, 75, 23);
		add(delB);
		
		
	}

	
	
	public String getLaser() {
		return laser;
	}
	public void setLaser(String laser) {
		this.laser = laser;
		
	}
	public String getIntensity() {
		return intensity;
	}
	public void setIntensity(String intensity) {
		
		this.intensity = intensity;
	}
	public int getIndex() {
		return index;
	}
	public void setIndex(int index) {
		this.index = index;
	}
	public boolean isSelected() {
		return isSelected;
	}
	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
		if(isSelected){
			selectFlag.setSelected(true);
			if(DSTEditor.instance != null){
				DSTEditor.instance.changeSelect(index);
			}
		}else{
			selectFlag.setSelected(false);
		}
	}
	
}
